package com.lsy.weizhi2.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.ImageAdapter2;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.customview.AutoGridView;
import com.lsy.weizhi2.customview.MyGridView;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.lsy.weizhi2.myutils.SendPhotoCall;
import com.lsy.weizhi2.picture.PicturePopup;
import com.lsy.weizhi2.picture.PictureUtils;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import java.io.File;
import java.util.Random;

public class SendActivity extends MyBaseActivity implements View.OnClickListener {
    private Context context;
    private MyTitleView send_activity_title;
    private TextView send_activity_classify, send_activity_send;
    private EditText send_activity_title_, send_activity_mail, send_activity_phone, send_activity_content_et;
    private MyGridView send_activity_agv;
    private ImageAdapter2 imageAdapter;
    private int xx;
    private Handler handler = new Handler();
    private int height;//title控件的高度，便于使用沉浸式效果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        context = SendActivity.this;
        Init();
    }

    private void Init() {
        send_activity_title = (MyTitleView) findViewById(R.id.send_activity_title);
        send_activity_classify = (TextView) findViewById(R.id.send_activity_classify);
        send_activity_send = (TextView) findViewById(R.id.send_activity_send);
        send_activity_title_ = (EditText) findViewById(R.id.send_activity_title_);
        send_activity_mail = (EditText) findViewById(R.id.send_activity_mail);
        send_activity_phone = (EditText) findViewById(R.id.send_activity_phone);
        send_activity_content_et = (EditText) findViewById(R.id.send_activity_content_et);
        send_activity_agv = (MyGridView) findViewById(R.id.send_activity_agv);
        imageAdapter = new ImageAdapter2(context, null);
        send_activity_agv.setAdapter(imageAdapter);
        send_activity_send.setOnClickListener(this);
        send_activity_classify.setOnClickListener(this);
        imageAdapter.setImageAdapterCall(new ImageAdapterCall() {
            @Override
            public void onClick(View view, final int x) {
                new PicturePopup().init(context, new SendPhotoCall() {
                    @Override
                    public void Request(String message) {
                        Log.i("SendActivity", "message===" + message);
                        // imageAdapter.UpData(x,null);
                    }
                });
            }

            @Override
            public void onDetele(View view, int x) {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = send_activity_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(send_activity_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                send_activity_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_activity_classify:
                CustomPage.Tag = 1;
                MyMethod.toActivity(context, CustomPage.class);
                break;
            case R.id.send_activity_send:
                getAllInfo();
                break;
        }
    }

    private void getAllInfo() {
        send_activity_title_.getText().toString();//获取主题
        send_activity_mail.getText().toString();//获取邮箱
        send_activity_phone.getText().toString();//获取电话号码
        send_activity_content_et.getText().toString();//获取内容
    }

    //请求权限的返回结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PictureUtils.CAMERA) {
            for (int index = 0; index < permissions.length; index++) {
                switch (permissions[index]) {
                    case Manifest.permission.CAMERA:
                        if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {
                            /**用户已经受权*/
                            startActivityForResult(
                                    new Intent(MediaStore.ACTION_IMAGE_CAPTURE), PictureUtils.CAMERA);
                        } else if (grantResults[index] == PackageManager.PERMISSION_DENIED) {
                            /**用户拒绝了权限*/
                            MyMethod.ShowDialog(context,"应用没有拍照权限，请授权！");
                        } else {
                            MyMethod.ShowDialog(context,"应用没有拍照权限，请授权！");
                        }
                        break;
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureUtils.CAMERA) {
            final Bitmap bitmap = data.getParcelableExtra("data");
            Log.i("SendNote", Environment.getExternalStorageDirectory().toString());
            String path = Environment.getExternalStorageDirectory() + File.separator + "paizhao/image/" + new Random().nextInt(10) + ".jpg";
            File file = new File(path);
            MyOkHttpUtils.getInstance(context).upLoadImage(Constants.UP_USER_PICTURE, file).excute(new MyOkHttpUtils.OKCallBack() {

                @Override
                public void onFailure(String message) {
                    Log.i("message", "F==" + message);
                    MyMethod.showToast(context, message);
                }

                @Override
                public void onResponse(String message) {
                }
            });

        }
    }
}
